package com.kelvin.paopao.module.main.find.find;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

import com.kelvin.kelvinlib.utils.CommonUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.adapter.MainFindListViewPagerAdapter;
import com.kelvin.paopao.module.base.BaseFragment;
import com.kelvin.paopao.module.main.find.filter.FindFilterActivity;
import com.kelvin.paopao.module.main.find.list.MainFindListFragment;
import com.kelvin.paopao.module.qrcode.scan.ScanActivity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/5/6.
 */
public class MainFindFragment extends BaseFragment {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;

    private String[] tabTv = {"新人", "附近", "免费", "推荐", "认证"};

    private static final int ACTION_TO_FILTER = 1001;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private MainFindListViewPagerAdapter viewPagerAdapter;
    private MainFindListFragment mainFindNewFragment, mainFindNearFragment, mainFindFreeFragment,
            mainFindTuijianFragment, mainFindRenzhengFragment;

    private List<Fragment> fragmentList;


    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_main_find;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        toolbar.setNavigationIcon(R.drawable.icon_scan);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到二维码界面
                ScanActivity.launch(getContext());
            }
        });
        toolbar.inflateMenu(R.menu.menu_find_filter);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                //打开筛选页面
                Intent intent = new Intent(getContext(), FindFilterActivity.class);
                startActivityForResult(intent, ACTION_TO_FILTER);
                return true;
            }
        });

        for (int i = 0; i < tabTv.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            tab.setText(tabTv[i]);
            tabLayout.addTab(tab);
        }

        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                setUpIndicatorWidth();
            }
        });
        viewPager.setOffscreenPageLimit(4);

        mainFindNewFragment = MainFindListFragment.newInstance(MainFindListFragment.TYPE_NEW);
        mainFindNearFragment = MainFindListFragment.newInstance(MainFindListFragment.TYPE_NEAR);
        mainFindFreeFragment = MainFindListFragment.newInstance(MainFindListFragment.TYPE_FREE);
        mainFindTuijianFragment = MainFindListFragment.newInstance(MainFindListFragment.TYPE_TUIJIAN);
        mainFindRenzhengFragment = MainFindListFragment.newInstance(MainFindListFragment.TYPE_RENZHENG);
        fragmentList = new ArrayList<>();
        fragmentList.add(mainFindNewFragment);
        fragmentList.add(mainFindNearFragment);
        fragmentList.add(mainFindFreeFragment);
        fragmentList.add(mainFindTuijianFragment);
        fragmentList.add(mainFindRenzhengFragment);
        viewPagerAdapter = new MainFindListViewPagerAdapter(getChildFragmentManager(), fragmentList);

        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == ACTION_TO_FILTER) {
                String sex = data.getStringExtra("sex");
                String height = data.getStringExtra("height");
                String weight = data.getStringExtra("weight");
                String user_label = data.getStringExtra("user_label");

                ((MainFindListFragment) fragmentList.get(viewPager.getCurrentItem())).getDataWithFilter(sex, weight, height, user_label);
//                mPresenter.getDataWithFilter(sex, weight, height, user_label);
            }
        }
    }

    private void setUpIndicatorWidth() {
        Class<?> tabLayoutClass = tabLayout.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayoutClass.getDeclaredField("mTabStrip");
            tabStrip.setAccessible(true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        LinearLayout layout = null;
        try {
            if (tabStrip != null) {
                layout = (LinearLayout) tabStrip.get(tabLayout);
            }
            for (int i = 0; i < layout.getChildCount(); i++) {
                View child = layout.getChildAt(i);
                child.setPadding(0, 0, 0, 0);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    params.setMarginStart(CommonUtil.dip2px(mContext, 20f));
                    params.setMarginEnd(CommonUtil.dip2px(mContext, 20f));
                }
                child.setLayoutParams(params);
                child.invalidate();
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

}